import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: MENG
 * Date: 2022-07-19
 * Time: 22:13
 */
class Student implements Comparable <Student> {
    public int age;


    @Override
    public int compareTo(Student o) {
        //升序排序
        return this.age - o.age;
    }

    public Student(int age) {
        this.age = age;
    }

}
class AgeComparator implements Comparator<Student>{

    //降序排列
    @Override
    public int compare(Student o1, Student o2) {
        return o2.age - o1.age;
    }
}


public class Test {
    //topk问题，第一种方法
    public static void topK1(int[] array,int k) {
        PriorityQueue<Integer> maxPQ = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                //重写compare，建立大根堆
                return o2 - o1;
            }
        });
        //添加元素进堆
        for (int i = 0; i < array.length; i++) {
            maxPQ.offer(array[i]);
        }

        //获取堆顶元素
        for (int i = 0; i < k; i++) {
            int val = maxPQ.poll();
            System.out.println(val);
        }

    }

    //建立大根堆的方式来，来获取k个最小元素
    public static int[] topK2(int[] array,int k) {
        PriorityQueue<Integer> maxPQ = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            //建立一个大根堆
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });

        for (int i = 0; i < array.length; i++) {
            if(maxPQ.size() < k){
                maxPQ.offer(array[i]);
            }else{
                int top = maxPQ.peek();
                if(array[i] < top){
                    maxPQ.poll();
                    maxPQ.offer(array[i]);
                }
            }

        }
        int[] ret = new int[k];
        for (int i = 0; i < k; i++) {
            ret[i] = maxPQ.poll();
        }
        return ret;
    }
    //测试堆排序
    public static void main(String[] args) {
        TestHeap testHeap = new TestHeap();
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        testHeap.initArray(array);
        testHeap.createHeap();
        testHeap.heapSort();

    }
    //测试topK
    public static void main3(String[] args) {
        TestHeap testHeap = new TestHeap();
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        testHeap.poll();
        //topK1(array,3);
        int[] ret = topK2(array,3);
        for (int i = 0; i < ret.length; i++) {
            System.out.println(ret[i]);
        }
    }

    public static void main2(String[] args) {
        TestHeap testHeap = new TestHeap();
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        testHeap.initArray(array);
        testHeap.createHeap();
//        testHeap.offer(80);

    }

    public static void main1(String[] args) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        priorityQueue.offer(12);
        priorityQueue.offer(5);
        priorityQueue.offer(42);

        priorityQueue.offer(8);
        System.out.println(priorityQueue.peek());
        Student student1 = new Student(18);
        Student student2 = new Student(20);

        AgeComparator ageComparator = new AgeComparator();
        PriorityQueue<Student> priorityQueue1 = new PriorityQueue<>(12,ageComparator);
        //PriorityQueue<Student> priorityQueue2 = new PriorityQueue<>(12);
        priorityQueue1.offer(student1);
        priorityQueue1.offer(student2);
        System.out.println(priorityQueue1.peek());

        String s = "hello";
        String s1 = "hello";
        System.out.println(s.equals(s1));

    }
}
